import Toast from '../../../component/Toast';
import { uuid } from '../../../util/code'
import { clone } from '../../../util/utils'
import { pagePermission, savePermission, removePermission, updatePermission } from '../service/permissionService';

const defaultState = {
	
	list: [],
	pageNum: undefined,
	pageSize: undefined,
	total: undefined,
	editBuf: {},
	
}

export default {

	namespace: 'permission',

	state: {...defaultState},

	reducers: {
		
		reset(state, {tab}) {
			return {...defaultState}
		},
		
		setList(state, {list}) {
			return {...state, ...{list}}
		},
		
		setPageNum(state, {pageNum}) {
			return {...state, ...{pageNum}}
		},
		
		setPageSize(state, {pageSize}) {
			return {...state, ...{pageSize}}
		},
		
		setTotal(state, {total}) {
			return {...state, ...{total}}
		},
		
		setEditBuf(state, {editBuf}) {
			return {...state, ...{editBuf}}
		},
		
		putEditBuf(state, {key, value}) {
			state = clone(state)
			state.editBuf[clone(key)] = clone(value)
			return state
		},
		
	},
	
	effects: {
		
		*flushRecord({record, key}, {select, put}) {
			let {list} = yield select(state => state.permission)
			// save时使用uuid的key
			key = key || record.key
			record.key = record.permissionId
			list = clone(list);
			for (let i = 0; i < list.length; i ++) {
				if (list[i] && list[i].key == key) {
					list.splice(i, 1, record)
					yield put({type: 'setList', list})
					return
				}
			}
		},
		
		*pagePermission({pageNum, pageSize}, {select, put}) {
			let res = yield pagePermission({pageNum, pageSize})
			if (res.code == 1) {
				let pageInfo = res.data
				if (pageInfo && pageInfo.list) {
					// 添加key字段
					for (let item of pageInfo.list) {
						if (!item.key)
							item.key = item.permissionId
					}
					yield put({type: 'setList', list: pageInfo.list})
					yield put({type: 'setTotal', total: pageInfo.total})
					yield put({type: 'setPageNum', pageNum: pageInfo.pageNum})
					yield put({type: 'setPageSize', pageSize: pageInfo.pageSize})
				}
			} else {
				res.msg && Toast.show(res.msg)
			}
		},
		
		*edit({record}, {select, put}) {
			let {list} = yield select(state => state.permission)
			list = clone(list)
			if (list) {
				let permission = findListByKey(list, record.key)
				if (permission) {
					// 存入缓存
					yield put({
						type: 'putEditBuf', 
						key: permission.key,
						value: permission
					})
					// 修改内容
					permission.editable = true
					yield put({type: 'setList', list})
				}
			}
		},
		
		*editRecord({record, key, value}, {select, put}) {
			let {list} = yield select(state => state.permission)
			let permission = findListByKey(list, record.key)
			if (permission) {
				permission[key] = value
				yield put({type: 'setList', list})
			}
		},
		
		*cancel({record}, {select, put}) {
			let {list, editBuf} = yield select(state => state.permission)
			list = clone(list)
			if (list && list.length > 0) {
				let permission
				let i = 0
				for (; i < list.length; i ++) {
					if (list[i] && list[i].key == record.key) {
						permission = list[i]
						break
					}
				}
				if (permission) {
					if (permission.permissionId) {
						let buf = editBuf[permission.key];
						// 同步回之前缓存
						for (let key in permission) {
							permission[key] = buf[key]
						}
						permission.editable = false
						// 删除buf
						editBuf = clone(editBuf)
						delete editBuf[permission.key]
						yield put({type: 'setEditBuf', editBuf})
					} else {
						// 新建的record
						// 删除
						list.splice(i, 1);
					}
					yield put({type: 'setList', list})
				}
			}
		},
		
		*save({key}, {select, put}) {
			let {list, editBuf, total} = yield select(state => state.permission)
			let record = findListByKey(list, key)
			if (record) {
				let res
				if (!record.permission) {
					return Toast.show('权限不能为空')
				}
				if (record.permissionId) {
					// update
					res = yield updatePermission(record)
				} else {
					// save
					res = yield savePermission(record)
					if (res.code == 1) {
						yield put({type: 'setTotal', total: total + 1})
					}
				}
				if (res.code == 1 && res.data) {
					// 局部刷新
					yield put({type: 'flushRecord', record: res.data, key})
				} else {
					Toast.show(res.msg)
				}
			}
		},
		
		*remove({permissionId}, {select, put}) {
			let res = yield removePermission({permissionId})
			if (res.code == 1) {
				let {list, total, pageNum, pageSize} = yield select(state => state.permission)
				if (list && list.length > 0) {
					for (let i = 0; i < list.length; i ++) {
						if (list[i] && list[i].key == permissionId) {
							list.splice(i, 1)
							yield put({type: 'setList', list})
							
							if (getrealLen(list) < pageSize && pageNum < Math.floor(total / pageSize) + (total % pageSize == 0 ? 0 : 1)) {
								// 当前不是最后一页,且list不足时刷新当前页
								yield put({type: 'pagePermission', pageNum: pageNum, pageSize})
							} else if (total % pageSize == 1 && pageNum > 1) {
								// 当前页不是第一页,且当前页list为空时刷新到前一页
								yield put({type: 'pagePermission', pageNum: -- pageNum, pageSize})
							} else {
								// 不刷新
								yield put({type: 'setTotal', total: -- total})
							}
							
							break
						}
					}
				}
			} else {
				Toast.show(res.msg)
			}
		},
		
		*add(action, {select, put}) {
			let {list} = yield select(state => state.permission)
			list = clone(list)
			if (list) {
				list.splice(0, 0, {key: uuid(), editable: true})
				yield put({type: 'setList', list})
			}
		},
		
	}

}

function findListByKey(list, key) {
	if (list && list.length > 0) {
		for (let i = 0; i < list.length; i ++) {
			if (list[i] && list[i].key == key)
				return list[i]
		}
	}
}

function getrealLen(list) {
	let len = 0
	for (let item of list) {
		if (item.permissionId) 
			len ++;
	}
	return len
}
